/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gsb.android;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import modele.DAO;
import modele.Medecin;

/**
 *
 * @author sio
 */
public class GsbMedecin extends ListActivity {

    protected void onListItemClick(ListView l, View v, int position, long id) {
        String tel = ((Medecin) l.getItemAtPosition(position)).getTel();
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:"+tel));
        startActivity(callIntent);
    }

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.listemed);
        TextView label = (TextView) findViewById(R.id.label);
        Intent inter = getIntent();
        String departement = inter.getStringExtra("leDep");
        label.setText(departement);

        final List<Medecin> lesMeds = DAO.getLesMeds(departement);
        MedAdapter adapter = new MedAdapter(lesMeds, this);
        setListAdapter(adapter);

        Button button = (Button) findViewById(R.id.filtrer);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                EditText edit = (EditText) findViewById(R.id.status);
                List<Medecin> lesMedsFiltres = new ArrayList<Medecin>();
                for (Medecin unMed : lesMeds) {
                    if (unMed.getNom().startsWith(edit.getText().toString())) {
                        lesMedsFiltres.add(unMed);
                    }
                }
                MedAdapter adapter = new MedAdapter(lesMedsFiltres, getApplicationContext());
                setListAdapter(adapter);
            }
        }
        );
    }
}
